﻿using System;
using System.ComponentModel.Design;
using System.Runtime.InteropServices;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using VSSDKAssist.Platform.ToolWindows;
using VSSDKAssist.Platform.FlavorProjects;

namespace VSSDKAssist.Platform
{
    [ProvideProjectFactory(typeof(VsPackageSetupProjectFactory), "Visual Studio Package Setup", "Visual Studio Package Setup Files (*.csproj);*.csproj", null, null, null, LanguageVsTemplate = "CSharp", NewProjectRequireNewFolderVsTemplate = true)]
    [PackageRegistration(UseManagedResourcesOnly = true)]
    [DefaultRegistryRoot("Software\\Microsoft\\VisualStudio\\9.0")]
    [ProvideMenuResource(1000, 1)]
    [Guid(VsPackageConstants.guidVsPackagePkgString)]
    [ProvideLoadKey("Standard", "1.0", "VSSDKAssist", "Ktulu", 1)]
    [InstalledProductRegistration(true, "#11289", "#29225", "1.0", IconResourceID = 26119, LanguageIndependentName = "VSSDK Assist")]
    [ProvideOptionPageAttribute(typeof(VSSDKAssist.Platform.OptionPages.GeneralOptionPage), "VSSDK Assist", "General", 2683, 27123, true)]
    [ProvideToolWindow(typeof(PackageBrowser))]
    public sealed class VsPackage : Package, Microsoft.VisualStudio.Shell.Interop.IVsInstalledProduct
    {
        public VsPackage()
        {
        }

        #region Package Members
        protected override void Initialize()
        {
            base.Initialize();
            AddPackageBrowserCommandHandler();
            this.RegisterProjectFactory(new VsPackageSetupProjectFactory(this));
        }
        #endregion

        private void ShowPackageBrowser(object sender, EventArgs e)
        {
            ToolWindowPane window = this.FindToolWindow(typeof(VSSDKAssist.Platform.ToolWindows.PackageBrowser), 0, true);

            if((window == null) || (window.Frame == null))
            {
                throw new COMException("Can not create window.");
            }

            IVsWindowFrame windowFrame = (IVsWindowFrame)window.Frame;
            Microsoft.VisualStudio.ErrorHandler.ThrowOnFailure(windowFrame.Show());
        }

        private void AddPackageBrowserCommandHandler()
        {
            OleMenuCommandService menuCommandService = GetService(typeof(IMenuCommandService)) as OleMenuCommandService;
            if(menuCommandService != null)
            {
                // Create the command for the menu item.
                CommandID menuCommandID = new CommandID(VSSDKAssist.Platform.Commands.PackageBrowserCommandConstants.guidPackageBrowserCommand, (int)VSSDKAssist.Platform.Commands.PackageBrowserCommandConstants.cmdidPackageBrowserCommand);
                MenuCommand menuItem = new MenuCommand(new EventHandler(PackageBrowserCommandCallback), menuCommandID);
                menuCommandService.AddCommand(menuItem);
            }
        }

        private void PackageBrowserCommandCallback(object sender, EventArgs e)
        {
            ShowPackageBrowser(sender, e);
        }

        public int IdBmpSplash(out uint pIdBmp)
        {
            pIdBmp = 0;
            return Microsoft.VisualStudio.VSConstants.S_OK;
        }

        public int IdIcoLogoForAboutbox(out uint pIdIco)
        {
            pIdIco = 26119;
            return Microsoft.VisualStudio.VSConstants.S_OK;
        }

        public int OfficialName(out string pbstrName)
        {
            pbstrName = GetResourceString("11289");
            return Microsoft.VisualStudio.VSConstants.S_OK;
        }

        public int ProductDetails(out string pbstrProductDetails)
        {
            pbstrProductDetails = GetResourceString("29225");
            return Microsoft.VisualStudio.VSConstants.S_OK;
        }

        public int ProductID(out string pbstrPID)
        {
            pbstrPID = GetResourceString("10856");
            return Microsoft.VisualStudio.VSConstants.S_OK;
        }

        private string GetResourceString(string resourceName)
        {
            string resourceValue;

            Microsoft.VisualStudio.Shell.Interop.IVsResourceManager resourceManager =
                (Microsoft.VisualStudio.Shell.Interop.IVsResourceManager)GetService(typeof(Microsoft.VisualStudio.Shell.Interop.SVsResourceManager));

            if(resourceManager == null)
            {
                throw new InvalidOperationException(
                    "Could not get SVsResourceManager service. Make sure that the package is sited before calling this method");
            }

            Guid packageGuid = this.GetType().GUID;
            int hr = resourceManager.LoadResourceString(
                ref packageGuid, -1, resourceName, out resourceValue);

            Microsoft.VisualStudio.ErrorHandler.ThrowOnFailure(hr);

            return resourceValue;
        }
    }
}